﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Client;
using SiemensOPC;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using VswService_V7.Utility;
using System.Collections.Concurrent;
using VswService_V7.DataBase;

namespace VswService_V7.OPC.DB
{
    public class ProcessParameterDbData : SubscriptionDbDataBase
    {
        public ProcessParameterDbData(SimaticOPCClient opc, NodeConfig nodes) : base(opc, nodes)
        {
            MachinePosition = nodes.MillConfig.GetPositionList();
            ProcessData_50 = new ProcessData(3);
            ProcessData_100 = new ProcessData(3);
            ProcessData_150 = new ProcessData(3);
            ProcessData_200 = new ProcessData(3);
            MillLineParameter = new MillLineParameter();

            MillLineNormal = false;
            startRecord = nodes.ProcessDataRecordOn;

            Task.Run(() => HandleMillParameter());
            //Task.Run(() => HandleProcessData_50());
            //Task.Run(() => HandleProcessData_100());
            //Task.Run(() => HandleProcessData_150());
            //Task.Run(() => HandleProcessData_200());
        }



        #region private property

        /// <summary>
        /// 根据这个位判断是否应该存储PROCESS DATA
        /// </summary>
        public bool MillLineNormal
        {
            set;
            get;
        }

        /// <summary>
        /// 根据这个位判断是否应该开启存储PROCESS DATA 功能
        /// </summary>
        private bool startRecord;


        List<int> MachinePosition;

        private MillLineParameter MillLineParameter;
        private ProcessData ProcessData_50;
        private ProcessData ProcessData_100;
        private ProcessData ProcessData_150;
        private ProcessData ProcessData_200;

        #endregion

        string lastStripNo = "0";

        public override void CreateSubscription()
        {
            opcServer.QuickAddMonitorItem(opcNodes.MillLineParameteraAddress.GrpoupName,
                opcNodes.MillLineParameteraAddress.PublishTime,
                opcNodes.MillLineParameteraAddress.SampleTime,
                opcNodes.MillLineParameteraAddress.GetNodeAddress(),
                opcNodes.MillLineParameteraAddress.GetNodeDisplayName(),
                MillLineParameterHandler);


            opcServer.QuickAddMonitorItem(opcNodes.MillLineProcessDataAddress_50.GrpoupName,
                opcNodes.MillLineProcessDataAddress_50.PublishTime,
                opcNodes.MillLineProcessDataAddress_50.SampleTime,
                opcNodes.MillLineProcessDataAddress_50.GetNodeAddress(),
                opcNodes.MillLineProcessDataAddress_50.GetNodeDisplayName(),
                ProcessDataHandler50);

            opcServer.QuickAddMonitorItem(opcNodes.MillLineProcessDataAddress_100.GrpoupName,
                opcNodes.MillLineProcessDataAddress_100.PublishTime,
                opcNodes.MillLineProcessDataAddress_100.SampleTime,
                opcNodes.MillLineProcessDataAddress_100.GetNodeAddress(),
                opcNodes.MillLineProcessDataAddress_100.GetNodeDisplayName(),
                ProcessDataHandler100);

            opcServer.QuickAddMonitorItem(opcNodes.MillLineProcessDataAddress_150.GrpoupName,
                opcNodes.MillLineProcessDataAddress_150.PublishTime,
                opcNodes.MillLineProcessDataAddress_150.SampleTime,
                opcNodes.MillLineProcessDataAddress_150.GetNodeAddress(),
                opcNodes.MillLineProcessDataAddress_150.GetNodeDisplayName(),
                ProcessDataHandler150);

            opcServer.QuickAddMonitorItem(opcNodes.MillLineProcessDataAddress_200.GrpoupName,
                opcNodes.MillLineProcessDataAddress_200.PublishTime,
                opcNodes.MillLineProcessDataAddress_200.SampleTime,
                opcNodes.MillLineProcessDataAddress_200.GetNodeAddress(),
                opcNodes.MillLineProcessDataAddress_200.GetNodeDisplayName(),
                ProcessDataHandler200);
        }


        private void MillLineParameterHandler(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;

            switch (monitoredItem.DisplayName)
            {


                case "Strip_1":
                    {
                        MillLineParameter.Strip_1 = notification.Value.Value.ToString();
                        if (MillLineParameter.Strip_1 != lastStripNo)
                        {
                            if(MillLineParameter.Strip_1.Length==11)
                            {
                                lastStripNo = MillLineParameter.Strip_1;
                                Task.Run(() => UpdateStripStatusAfterLastPipe());
                            }
                        }
                        break;
                    }

                case "Strip_2":
                    {
                        MillLineParameter.Strip_2 = notification.Value.Value.ToString();
                        break;
                    }

                case "Strip_3":
                    {
                        MillLineParameter.Strip_3 = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_VEE_Length":
                    {
                        MillLineParameter.HFW_VEE_Length = notification.Value.Value.ToString();

                        break;
                    }

                case "HFW_Panel_Cooling":
                    {
                        MillLineParameter.HFW_Panel_Cooling = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT_Panel_Cooling":
                    {
                        MillLineParameter.LHT_Panel_Cooling = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT_Inductor_Cooling":
                    {
                        MillLineParameter.LHT_Inductor_Cooling = notification.Value.Value.ToString();
                        break;
                    }

                case "JA_Panel_Cooling":
                    {
                        MillLineParameter.JA_Panel_Cooling = notification.Value.Value.ToString();
                        break;
                    }

                case "JA_Inductor_Cooling":
                    {
                        MillLineParameter.JA_Inductor_Cooling = notification.Value.Value.ToString();
                        break;
                    }

                case "LineSpeed":
                    {
                        MillLineParameter.LineSpeed = notification.Value.Value.ToString();
                        break;
                    }

                case "QT":
                    {
                        MillLineParameter.QT = notification.Value.Value.ToString();
                        break;
                    }

                default:
                    break;


            }
        }

        volatile bool processData50Interlock = false;
        private void ProcessDataHandler50(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;

            ProcessData_50.updateTime = notification.Value.SourceTimestamp;
            switch (monitoredItem.DisplayName)
            {
                case "Dtj_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(),out double dtj);
                        if(result)
                        {
                            ProcessData_50.Dtj[0] = dtj;
                        }
                        else
                        {
                            ProcessData_50.Dtj[0] = 0;
                        }

                        if(!processData50Interlock)
                        {
                            processData50Interlock = true;
                            Task.Run(() => HandleProcessData_50());
                        }

                        break;
                    }

                case "Dtj_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_50.Dtj[1] = dtj;
                        }
                        else
                        {
                            ProcessData_50.Dtj[1] = 0;
                        }
                        break;
                    }

                case "Dtj_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_50.Dtj[2] = dtj;
                        }
                        else
                        {
                            ProcessData_50.Dtj[2] = 0;
                        }
                        break;
                    }


                case "StripStart_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_50.StripStart[0] = stripStart;
                        }
                        else
                        {
                            ProcessData_50.StripStart[0] = 0;
                        }
                        break;
                    }

                case "StripStart_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_50.StripStart[1] = stripStart;
                        }
                        else
                        {
                            ProcessData_50.StripStart[1] = 0;
                        }
                        break;
                    }

                case "StripStart_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_50.StripStart[2] = stripStart;
                        }
                        else
                        {
                            ProcessData_50.StripStart[2] = 0;
                        }
                        break;
                    }

                case "StripEnd_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_50.StripEnd[0] = stripEnd;
                        }
                        else
                        {
                            ProcessData_50.StripEnd[0] = -99;
                        }
                        break;
                    }

                case "StripEnd_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_50.StripEnd[1] = stripEnd;
                        }
                        else
                        {
                            ProcessData_50.StripEnd[1] = -99;
                        }
                        break;
                    }

                case "StripEnd_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_50.StripEnd[2] = stripEnd;
                        }
                        else
                        {
                            ProcessData_50.StripEnd[2] = -99;
                        }
                        break;
                    }


                case "HFW_Power":
                    {
                        ProcessData_50.HFW_Power = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Freq":
                    {
                        ProcessData_50.HFW_Freq = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Tempe":
                    {
                        ProcessData_50.HFW_Tempe = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Curt":
                    {
                        ProcessData_50.HFW_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Volt":
                    {
                        ProcessData_50.HFW_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT1_Power":
                    {
                        ProcessData_50.LHT1_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Freq":
                    {
                        ProcessData_50.LHT1_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Tempe":
                    {
                        ProcessData_50.LHT1_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Curt":
                    {
                        ProcessData_50.LHT1_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT1_Volt":
                    {
                        ProcessData_50.LHT1_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT2_Power":
                    {
                        ProcessData_50.LHT2_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Freq":
                    {
                        ProcessData_50.LHT2_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Tempe":
                    {
                        ProcessData_50.LHT2_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Curt":
                    {
                        ProcessData_50.LHT2_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT2_Volt":
                    {
                        ProcessData_50.LHT2_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT3_Power":
                    {
                        ProcessData_50.LHT3_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Freq":
                    {
                        ProcessData_50.LHT3_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Tempe":
                    {
                        ProcessData_50.LHT3_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Curt":
                    {
                        ProcessData_50.LHT3_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT3_Volt":
                    {
                        ProcessData_50.LHT3_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT4_Power":
                    {
                        ProcessData_50.LHT4_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Freq":
                    {
                        ProcessData_50.LHT4_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Tempe":
                    {
                        ProcessData_50.LHT4_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Curt":
                    {
                        ProcessData_50.LHT4_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT4_Volt":
                    {
                        ProcessData_50.LHT4_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "QC":
                    {
                        ProcessData_50.QC = notification.Value.Value.ToString();
                        break;
                    }


                case "SQ_1":
                    {
                        ProcessData_50.SQ_1 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_2":
                    {
                        ProcessData_50.SQ_2 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_3":
                    {
                        ProcessData_50.SQ_3 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_4":
                    {
                        ProcessData_50.SQ_4 = notification.Value.Value.ToString();
                        break;
                    }

                default:
                    break;


            }
        }

        volatile bool processData100Interlock = false;
        private void ProcessDataHandler100(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;

            ProcessData_100.updateTime = notification.Value.SourceTimestamp;
            switch (monitoredItem.DisplayName)
            {
                case "Dtj_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_100.Dtj[0] = dtj;
                        }
                        else
                        {
                            ProcessData_100.Dtj[0] = 0;
                        }

                        if (!processData100Interlock)
                        {
                            processData100Interlock = true;
                            Task.Run(() => HandleProcessData_100());
                        }

                        break;
                    }

                case "Dtj_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_100.Dtj[1] = dtj;
                        }
                        else
                        {
                            ProcessData_100.Dtj[1] = 0;
                        }
                        break;
                    }

                case "Dtj_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_100.Dtj[2] = dtj;
                        }
                        else
                        {
                            ProcessData_100.Dtj[2] = 0;
                        }
                        break;
                    }

                case "StripStart_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_100.StripStart[0] = stripStart;
                        }
                        else
                        {
                            ProcessData_100.StripStart[0] = 0;
                        }
                        break;
                    }

                case "StripStart_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_100.StripStart[1] = stripStart;
                        }
                        else
                        {
                            ProcessData_100.StripStart[1] = 0;
                        }
                        break;
                    }

                case "StripStart_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_100.StripStart[2] = stripStart;
                        }
                        else
                        {
                            ProcessData_100.StripStart[2] = 0;
                        }
                        break;
                    }

                case "StripEnd_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_100.StripEnd[0] = stripEnd;
                        }
                        else
                        {
                            ProcessData_100.StripEnd[0] = -99;
                        }
                        break;
                    }

                case "StripEnd_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_100.StripEnd[1] = stripEnd;
                        }
                        else
                        {
                            ProcessData_100.StripEnd[1] = -99;
                        }
                        break;
                    }

                case "StripEnd_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_100.StripEnd[2] = stripEnd;
                        }
                        else
                        {
                            ProcessData_100.StripEnd[2] = -99;
                        }
                        break;
                    }

                case "HFW_Power":
                    {
                        ProcessData_100.HFW_Power = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Freq":
                    {
                        ProcessData_100.HFW_Freq = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Tempe":
                    {
                        ProcessData_100.HFW_Tempe = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Curt":
                    {
                        ProcessData_100.HFW_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Volt":
                    {
                        ProcessData_100.HFW_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT1_Power":
                    {
                        ProcessData_100.LHT1_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Freq":
                    {
                        ProcessData_100.LHT1_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Tempe":
                    {
                        ProcessData_100.LHT1_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Curt":
                    {
                        ProcessData_100.LHT1_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT1_Volt":
                    {
                        ProcessData_100.LHT1_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT2_Power":
                    {
                        ProcessData_100.LHT2_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Freq":
                    {
                        ProcessData_100.LHT2_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Tempe":
                    {
                        ProcessData_100.LHT2_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Curt":
                    {
                        ProcessData_100.LHT2_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT2_Volt":
                    {
                        ProcessData_100.LHT2_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT3_Power":
                    {
                        ProcessData_100.LHT3_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Freq":
                    {
                        ProcessData_100.LHT3_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Tempe":
                    {
                        ProcessData_100.LHT3_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Curt":
                    {
                        ProcessData_100.LHT3_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT3_Volt":
                    {
                        ProcessData_100.LHT3_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT4_Power":
                    {
                        ProcessData_100.LHT4_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Freq":
                    {
                        ProcessData_100.LHT4_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Tempe":
                    {
                        ProcessData_100.LHT4_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Curt":
                    {
                        ProcessData_100.LHT4_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT4_Volt":
                    {
                        ProcessData_100.LHT4_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "QC":
                    {
                        ProcessData_100.QC = notification.Value.Value.ToString();
                        break;
                    }


                case "SQ_1":
                    {
                        ProcessData_100.SQ_1 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_2":
                    {
                        ProcessData_100.SQ_2 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_3":
                    {
                        ProcessData_100.SQ_3 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_4":
                    {
                        ProcessData_100.SQ_4 = notification.Value.Value.ToString();
                        break;
                    }

                default:
                    break;


            }
        }

        volatile bool processData150Interlock = false;
        private void ProcessDataHandler150(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;

            ProcessData_150.updateTime = notification.Value.SourceTimestamp;
            switch (monitoredItem.DisplayName)
            {
                case "Dtj_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_150.Dtj[0] = dtj;
                        }
                        else
                        {
                            ProcessData_150.Dtj[0] = 0;
                        }

                        if (!processData150Interlock)
                        {
                            processData150Interlock = true;
                            Task.Run(() => HandleProcessData_150());
                        }

                        break;
                    }

                case "Dtj_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_150.Dtj[1] = dtj;
                        }
                        else
                        {
                            ProcessData_150.Dtj[1] = 0;
                        }
                        break;
                    }

                case "Dtj_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_150.Dtj[2] = dtj;
                        }
                        else
                        {
                            ProcessData_150.Dtj[2] = 0;
                        }
                        break;
                    }

                case "StripStart_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_150.StripStart[0] = stripStart;
                        }
                        else
                        {
                            ProcessData_150.StripStart[0] = 0;
                        }
                        break;
                    }

                case "StripStart_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_150.StripStart[1] = stripStart;
                        }
                        else
                        {
                            ProcessData_150.StripStart[1] = 0;
                        }
                        break;
                    }

                case "StripStart_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_150.StripStart[2] = stripStart;
                        }
                        else
                        {
                            ProcessData_150.StripStart[2] = 0;
                        }
                        break;
                    }

                case "StripEnd_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_150.StripEnd[0] = stripEnd;
                        }
                        else
                        {
                            ProcessData_150.StripEnd[0] = -99;
                        }
                        break;
                    }

                case "StripEnd_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_150.StripEnd[1] = stripEnd;
                        }
                        else
                        {
                            ProcessData_150.StripEnd[1] = -99;
                        }
                        break;
                    }

                case "StripEnd_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_150.StripEnd[2] = stripEnd;
                        }
                        else
                        {
                            ProcessData_150.StripEnd[2] = -99;
                        }
                        break;
                    }

                case "HFW_Power":
                    {
                        ProcessData_150.HFW_Power = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Freq":
                    {
                        ProcessData_150.HFW_Freq = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Tempe":
                    {
                        ProcessData_150.HFW_Tempe = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Curt":
                    {
                        ProcessData_150.HFW_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Volt":
                    {
                        ProcessData_150.HFW_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT1_Power":
                    {
                        ProcessData_150.LHT1_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Freq":
                    {
                        ProcessData_150.LHT1_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Tempe":
                    {
                        ProcessData_150.LHT1_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Curt":
                    {
                        ProcessData_150.LHT1_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT1_Volt":
                    {
                        ProcessData_150.LHT1_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT2_Power":
                    {
                        ProcessData_150.LHT2_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Freq":
                    {
                        ProcessData_150.LHT2_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Tempe":
                    {
                        ProcessData_150.LHT2_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Curt":
                    {
                        ProcessData_150.LHT2_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT2_Volt":
                    {
                        ProcessData_150.LHT2_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT3_Power":
                    {
                        ProcessData_150.LHT3_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Freq":
                    {
                        ProcessData_150.LHT3_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Tempe":
                    {
                        ProcessData_150.LHT3_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Curt":
                    {
                        ProcessData_150.LHT3_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT3_Volt":
                    {
                        ProcessData_150.LHT3_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT4_Power":
                    {
                        ProcessData_150.LHT4_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Freq":
                    {
                        ProcessData_150.LHT4_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Tempe":
                    {
                        ProcessData_150.LHT4_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Curt":
                    {
                        ProcessData_150.LHT4_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT4_Volt":
                    {
                        ProcessData_150.LHT4_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "QC":
                    {
                        ProcessData_150.QC = notification.Value.Value.ToString();
                        break;
                    }


                case "SQ_1":
                    {
                        ProcessData_150.SQ_1 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_2":
                    {
                        ProcessData_150.SQ_2 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_3":
                    {
                        ProcessData_150.SQ_3 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_4":
                    {
                        ProcessData_150.SQ_4 = notification.Value.Value.ToString();
                        break;
                    }

                default:
                    break;


            }
        }

        volatile bool processData200Interlock = false;
        private void ProcessDataHandler200(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;

            ProcessData_200.updateTime = notification.Value.SourceTimestamp;
            switch (monitoredItem.DisplayName)
            {
                case "Dtj_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_200.Dtj[0] = dtj;
                        }
                        else
                        {
                            ProcessData_200.Dtj[0] = 0;
                        }

                        if (!processData200Interlock)
                        {
                            processData200Interlock = true;
                            Task.Run(() => HandleProcessData_200());
                        }

                        break;
                    }

                case "Dtj_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_200.Dtj[1] = dtj;
                        }
                        else
                        {
                            ProcessData_200.Dtj[1] = 0;
                        }
                        break;
                    }

                case "Dtj_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double dtj);
                        if (result)
                        {
                            ProcessData_200.Dtj[2] = dtj;
                        }
                        else
                        {
                            ProcessData_200.Dtj[2] = 0;
                        }
                        break;
                    }

                case "StripStart_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_200.StripStart[0] = stripStart;
                        }
                        else
                        {
                            ProcessData_200.StripStart[0] = 0;
                        }
                        break;
                    }

                case "StripStart_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_200.StripStart[1] = stripStart;
                        }
                        else
                        {
                            ProcessData_200.StripStart[1] = 0;
                        }
                        break;
                    }

                case "StripStart_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripStart);
                        if (result)
                        {
                            ProcessData_200.StripStart[2] = stripStart;
                        }
                        else
                        {
                            ProcessData_200.StripStart[2] = 0;
                        }
                        break;
                    }

                case "StripEnd_1":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_200.StripEnd[0] = stripEnd;
                        }
                        else
                        {
                            ProcessData_200.StripEnd[0] = -99;
                        }
                        break;
                    }

                case "StripEnd_2":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_200.StripEnd[1] = stripEnd;
                        }
                        else
                        {
                            ProcessData_200.StripEnd[1] = -99;
                        }
                        break;
                    }

                case "StripEnd_3":
                    {
                        bool result = double.TryParse(notification.Value.Value.ToString(), out double stripEnd);
                        if (result)
                        {
                            ProcessData_200.StripEnd[2] = stripEnd;
                        }
                        else
                        {
                            ProcessData_200.StripEnd[2] = -99;
                        }
                        break;
                    }

                case "HFW_Power":
                    {
                        ProcessData_200.HFW_Power = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Freq":
                    {
                        ProcessData_200.HFW_Freq = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Tempe":
                    {
                        ProcessData_200.HFW_Tempe = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Curt":
                    {
                        ProcessData_200.HFW_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "HFW_Volt":
                    {
                        ProcessData_200.HFW_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT1_Power":
                    {
                        ProcessData_200.LHT1_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Freq":
                    {
                        ProcessData_200.LHT1_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Tempe":
                    {
                        ProcessData_200.LHT1_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT1_Curt":
                    {
                        ProcessData_200.LHT1_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT1_Volt":
                    {
                        ProcessData_200.LHT1_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT2_Power":
                    {
                        ProcessData_200.LHT2_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Freq":
                    {
                        ProcessData_200.LHT2_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Tempe":
                    {
                        ProcessData_200.LHT2_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT2_Curt":
                    {
                        ProcessData_200.LHT2_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT2_Volt":
                    {
                        ProcessData_200.LHT2_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT3_Power":
                    {
                        ProcessData_200.LHT3_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Freq":
                    {
                        ProcessData_200.LHT3_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Tempe":
                    {
                        ProcessData_200.LHT3_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT3_Curt":
                    {
                        ProcessData_200.LHT3_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT3_Volt":
                    {
                        ProcessData_200.LHT3_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT4_Power":
                    {
                        ProcessData_200.LHT4_Power = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Freq":
                    {
                        ProcessData_200.LHT4_Freq = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Tempe":
                    {
                        ProcessData_200.LHT4_Tempe = notification.Value.Value.ToString();
                        break;
                    }
                case "LHT4_Curt":
                    {
                        ProcessData_200.LHT4_Curt = notification.Value.Value.ToString();
                        break;
                    }

                case "LHT4_Volt":
                    {
                        ProcessData_200.LHT4_Volt = notification.Value.Value.ToString();
                        break;
                    }

                case "QC":
                    {
                        ProcessData_200.QC = notification.Value.Value.ToString();
                        break;
                    }


                case "SQ_1":
                    {
                        ProcessData_200.SQ_1 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_2":
                    {
                        ProcessData_200.SQ_2 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_3":
                    {
                        ProcessData_200.SQ_3 = notification.Value.Value.ToString();
                        break;
                    }

                case "SQ_4":
                    {
                        ProcessData_200.SQ_4 = notification.Value.Value.ToString();
                        break;
                    }

                default:
                    break;


            }
        }

        private async Task HandleMillParameter()
        {
            //OPC 启动后5秒开始记录
            await Task.Delay(5000);
            while (true)
            {
                if (MillLineNormal && startRecord)
                {
                    try
                    {
                        MillLineParameter.updateTime = DateTime.Now;
                        MILL_PARAMETER.InsertMillParameter(MillLineParameter);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog("HandleMillParameter", ex);
                    }

                }
                //2 秒记录一次
                await Task.Delay(2000);
            }

        }

        private async Task HandleProcessData_50()
        {
            await Task.Delay(100);
            if (MillLineNormal && startRecord)
            {
                List<StripPosition> sp = ProcessData_50.GetRecordMachine(MillLineParameter.GetStripNoList(), MachinePosition);
                if (sp.Count > 0)
                {
                    StringBuilder sqlBuilder = new StringBuilder();
                    sqlBuilder.Append("insert all ");
                    for (int i = 0; i < sp.Count; i++)
                    {
                        string stripNo = sp[i].stripNumber;
                        string position = sp[i].position.ToString();
                        int machineId = sp[i].machineId;
                        switch (machineId)
                        {
                            
                            case 0:
                                {
                                    sqlBuilder.Append(STRIP_HFW.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
                                    break;
                                }

                            case 1:
                                {
                                    sqlBuilder.Append(STRIP_SQ.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
                                    break;
                                }

                            case 2:
                                {
                                    sqlBuilder.Append(STRIP_LHT1.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
                                    break;
                                }

                            case 3:
                                {
                                    sqlBuilder.Append(STRIP_LHT2.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
                                    break;
                                }

                            case 4:
                                {
                                    sqlBuilder.Append(STRIP_LHT3.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
                                    break;
                                }

                            case 5:
                                {
                                    sqlBuilder.Append(STRIP_LHT4.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
                                    break;
                                }

                            case 6:
                                {
                                    sqlBuilder.Append(STRIP_SUST.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }
                    sqlBuilder.Append("select 1 from dual");
                    string sql = sqlBuilder.ToString();
                    try
                    {
                        OracleHelper.ExecuteSqlList(sql);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog("HandleProcessData_50", ex);
                       
                    }                   
                }
            }
            processData50Interlock = false;
        }

        private async Task HandleProcessData_100()
        {
            await Task.Delay(100);
            if (MillLineNormal && startRecord)
            {
                List<StripPosition> sp = ProcessData_100.GetRecordMachine(MillLineParameter.GetStripNoList(), MachinePosition);
                if (sp.Count > 0)
                {
                    StringBuilder sqlBuilder = new StringBuilder();
                    sqlBuilder.Append("insert all ");
                    for (int i = 0; i < sp.Count; i++)
                    {
                        string stripNo = sp[i].stripNumber;
                        string position = sp[i].position.ToString();
                        int machineId = sp[i].machineId;
                        switch (machineId)
                        {

                            case 0:
                                {
                                    sqlBuilder.Append(STRIP_HFW.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_100));
                                    break;
                                }

                            case 1:
                                {
                                    sqlBuilder.Append(STRIP_SQ.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_100));
                                    break;
                                }

                            case 2:
                                {
                                    sqlBuilder.Append(STRIP_LHT1.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_100));
                                    break;
                                }

                            case 3:
                                {
                                    sqlBuilder.Append(STRIP_LHT2.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_100));
                                    break;
                                }

                            case 4:
                                {
                                    sqlBuilder.Append(STRIP_LHT3.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_100));
                                    break;
                                }

                            case 5:
                                {
                                    sqlBuilder.Append(STRIP_LHT4.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_100));
                                    break;
                                }

                            case 6:
                                {
                                    sqlBuilder.Append(STRIP_SUST.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_100));
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }
                    sqlBuilder.Append("select 1 from dual");
                    string sql = sqlBuilder.ToString();
                    try
                    {
                        OracleHelper.ExecuteSqlList(sql);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog("HandleProcessData_100", ex);
                    }
                    processData100Interlock = false;
                }
            }

        }

        private async Task HandleProcessData_150()
        {
            await Task.Delay(100);
            if (MillLineNormal && startRecord)
            {
                List<StripPosition> sp = ProcessData_150.GetRecordMachine(MillLineParameter.GetStripNoList(), MachinePosition);
                if (sp.Count > 0)
                {
                    StringBuilder sqlBuilder = new StringBuilder();
                    sqlBuilder.Append("insert all ");
                    for (int i = 0; i < sp.Count; i++)
                    {
                        string stripNo = sp[i].stripNumber;
                        string position = sp[i].position.ToString();
                        int machineId = sp[i].machineId;
                        switch (machineId)
                        {

                            case 0:
                                {
                                    sqlBuilder.Append(STRIP_HFW.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_150));
                                    break;
                                }

                            case 1:
                                {
                                    sqlBuilder.Append(STRIP_SQ.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_150));
                                    break;
                                }

                            case 2:
                                {
                                    sqlBuilder.Append(STRIP_LHT1.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_150));
                                    break;
                                }

                            case 3:
                                {
                                    sqlBuilder.Append(STRIP_LHT2.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_150));
                                    break;
                                }

                            case 4:
                                {
                                    sqlBuilder.Append(STRIP_LHT3.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_150));
                                    break;
                                }

                            case 5:
                                {
                                    sqlBuilder.Append(STRIP_LHT4.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_150));
                                    break;
                                }

                            case 6:
                                {
                                    sqlBuilder.Append(STRIP_SUST.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_150));
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }
                    sqlBuilder.Append("select 1 from dual");
                    string sql = sqlBuilder.ToString();
                    try
                    {
                        OracleHelper.ExecuteSqlList(sql);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog("HandleProcessData_150", ex);
                    }
                    processData150Interlock = false;
                }
            }

        }

        private async Task HandleProcessData_200()
        {
            await Task.Delay(100);
            if (MillLineNormal && startRecord)
            {
                List<StripPosition> sp = ProcessData_200.GetRecordMachine(MillLineParameter.GetStripNoList(), MachinePosition);
                if (sp.Count > 0)
                {
                    StringBuilder sqlBuilder = new StringBuilder();
                    sqlBuilder.Append("insert all ");
                    for (int i = 0; i < sp.Count; i++)
                    {
                        string stripNo = sp[i].stripNumber;
                        string position = sp[i].position.ToString();
                        int machineId = sp[i].machineId;
                        switch (machineId)
                        {

                            case 0:
                                {
                                    sqlBuilder.Append(STRIP_HFW.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_200));
                                    break;
                                }

                            case 1:
                                {
                                    sqlBuilder.Append(STRIP_SQ.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_200));
                                    break;
                                }

                            case 2:
                                {
                                    sqlBuilder.Append(STRIP_LHT1.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_200));
                                    break;
                                }

                            case 3:
                                {
                                    sqlBuilder.Append(STRIP_LHT2.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_200));
                                    break;
                                }

                            case 4:
                                {
                                    sqlBuilder.Append(STRIP_LHT3.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_200));
                                    break;
                                }

                            case 5:
                                {
                                    sqlBuilder.Append(STRIP_LHT4.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_200));
                                    break;
                                }

                            case 6:
                                {
                                    sqlBuilder.Append(STRIP_SUST.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_200));
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }
                    sqlBuilder.Append("select 1 from dual");
                    string sql = sqlBuilder.ToString();
                    try
                    {
                        OracleHelper.ExecuteSqlList(sql);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog("HandleProcessData_200", ex);
                    }
                    processData200Interlock = false;
                }
            }

        }

        private async Task UpdateStripStatusAfterLastPipe()
        {
            await Task.Delay(1000);
            try
            {
                //改变strip状态为已完成
                STRIP_INFO.UpdateStripStatusAfterFCO(lastStripNo);
                //判断task状态并改变
                TASK_INFO.UpdateTaskStatus(lastStripNo);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("updateStripStatusAfterLastPipe", ex);
            }
        }


        //backup
        //private async Task HandleProcessData()
        //{
        //    //OPC 启动后5秒开始记录
        //    await Task.Delay(5000);
        //    while (true)
        //    {
        //        if (MillLineNormal && startRecord)
        //        {
        //            List<StripPosition> sp = ProcessData_50.GetStripLocation(MillLineParameter.GetStripNoList(), MachinePosition);
        //            if (sp.Count > 0)
        //            {
        //                StringBuilder sqlBuilder = new StringBuilder();
        //                sqlBuilder.Append("insert all ");
        //                for (int i = 0; i < sp.Count; i++)
        //                {
        //                    string stripNo = sp[i].stripNumber;
        //                    string position = sp[i].position.ToString();
        //                    switch (i)
        //                    {
        //                        case 1:
        //                            {
        //                                sqlBuilder.Append(STRIP_SQ.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
        //                                break;
        //                            }

        //                        case 2:
        //                            {
        //                                sqlBuilder.Append(STRIP_HFW.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
        //                                break;
        //                            }

        //                        case 3:
        //                            {
        //                                sqlBuilder.Append(STRIP_LHT1.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
        //                                break;
        //                            }

        //                        case 4:
        //                            {
        //                                sqlBuilder.Append(STRIP_LHT2.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
        //                                break;
        //                            }

        //                        case 5:
        //                            {
        //                                sqlBuilder.Append(STRIP_LHT3.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
        //                                break;
        //                            }

        //                        case 6:
        //                            {
        //                                sqlBuilder.Append(STRIP_LHT4.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
        //                                break;
        //                            }

        //                        case 7:
        //                            {
        //                                sqlBuilder.Append(STRIP_SUST.CreatInsertAllsql(sp[i].stripNumber, sp[i].position.ToString(), ProcessData_50));
        //                                break;
        //                            }
        //                        default:
        //                            {
        //                                break;
        //                            }
        //                    }
        //                }
        //                sqlBuilder.Append("select 1 from dual");
        //                string sql = sqlBuilder.ToString();
        //                try
        //                {
        //                    OracleHelper.ExecuteSqlList(sql);
        //                }
        //                catch (Exception ex)
        //                {
        //                    LogHelper.WriteLog("HandleProcessData_50", ex);
        //                }

        //            }
        //        }

        //        await Task.Delay(150);
        //    }

        //}
    }

    public class MillLineParameter
    {
        public MillLineParameter()
        {
            updateTime = new DateTime();

            Strip_1 = "0";
            Strip_2 = "0";
            Strip_3 = "0";

            HFW_VEE_Length = "0";
            HFW_Panel_Cooling = "0";

            LHT_Panel_Cooling = "0";
            LHT_Inductor_Cooling = "0";

            JA_Panel_Cooling = "0";
            JA_Inductor_Cooling = "0";

            LineSpeed = "0";
            QT = "0";
        }

        public DateTime updateTime;

        public string Strip_1 { get; set; }

        public string Strip_2 { get; set; }

        public string Strip_3 { get; set; }

        public string HFW_VEE_Length { get; set; }

        public string HFW_Panel_Cooling { get; set; }

        public string LHT_Panel_Cooling { get; set; }

        public string LHT_Inductor_Cooling { get; set; }

        public string JA_Panel_Cooling { get; set; }

        public string JA_Inductor_Cooling { get; set; }

        public string LineSpeed { get; set; }

        public string QT { get; set; }

        public List<string> GetStripNoList()
        {
            List<string> sl = new List<string>();
            if (!string.IsNullOrEmpty(Strip_1) && !Strip_1.Equals("0"))
            {
                sl.Add(Strip_1);
                if (!string.IsNullOrEmpty(Strip_2) && !Strip_2.Equals("0"))
                {
                    sl.Add(Strip_2);
                    if (!string.IsNullOrEmpty(Strip_3) && !Strip_3.Equals("0"))
                    {
                        sl.Add(Strip_3);
                    }
                }
                
            }
            return sl;
        }

    }

    public class ProcessData
    {
        /// <summary>
        /// 初始化需要计算的DTJ 数量
        /// </summary>
        /// <param name="dtjCount"></param>
        public ProcessData(int dtjCount)
        {
            updateTime = DateTime.Now.AddDays(-1);

            Dtj = new double[dtjCount];
            StripStart = new double[dtjCount];
            StripEnd = new double[dtjCount];
            for (int i = 0; i < dtjCount; i++)
            {
                Dtj[i] = 0;
                StripStart[i] = -1;
                StripEnd[i] = -1;
            }

            SetDtj = new bool[dtjCount];
            for (int i = 0; i < dtjCount; i++)
            {
                SetDtj[i] = false;
            }

            HFW_Power = "0";
            HFW_Freq = "0";
            HFW_Tempe = "0";
            HFW_Curt = "0";
            HFW_Volt = "0";

            LHT1_Power = "0";
            LHT1_Freq = "0";
            LHT1_Tempe = "0";
            LHT1_Curt = "0";
            LHT1_Volt = "0";

            LHT2_Power = "0";
            LHT2_Freq = "0";
            LHT2_Tempe = "0";
            LHT2_Curt = "0";
            LHT2_Volt = "0";

            LHT3_Power = "0";
            LHT3_Freq = "0";
            LHT3_Tempe = "0";
            LHT3_Curt = "0";
            LHT3_Volt = "0";

            LHT4_Power = "0";
            LHT4_Freq = "0";
            LHT4_Tempe = "0";
            LHT4_Curt = "0";
            LHT4_Volt = "0";

            QC = "0";

            SQ_1 = "0";
            SQ_2 = "0";
            SQ_3 = "0";
            SQ_4 = "0";

        }

        public DateTime updateTime;

        public bool[] SetDtj;
        public double[] Dtj;

        public double[] StripStart;
        public double[] StripEnd;
        

        public bool SetHFW_Power = false;
        public string HFW_Power;

        public bool SetHFW_Freq = false;
        public string HFW_Freq;

        public bool SetHFW_Tempe = false;
        public string HFW_Tempe;

        public bool SetHFW_Curt = false;
        public string HFW_Curt;

        public bool SetHFW_Volt = false;
        public string HFW_Volt;

        public bool SetLHT1_Power = false;
        public string LHT1_Power;

        public bool SetLHT1_Freq = false;
        public string LHT1_Freq;

        public bool SetLHT1_Tempe = false;
        public string LHT1_Tempe;

        public bool SetLHT1_Curt = false;
        public string LHT1_Curt;

        public bool SetLHT1_Volt = false;
        public string LHT1_Volt;

        public bool SetLHT2_Power = false;
        public string LHT2_Power;

        public bool SetLHT2_Freq = false;
        public string LHT2_Freq;

        public bool SetLHT2_Tempe = false;
        public string LHT2_Tempe;

        public bool SetLHT2_Curt = false;
        public string LHT2_Curt;

        public bool SetLHT2_Volt = false;
        public string LHT2_Volt;

        public bool SetLHT3_Power = false;
        public string LHT3_Power;

        public bool SetLHT3_Freq = false;
        public string LHT3_Freq;

        public bool SetLHT3_Tempe = false;
        public string LHT3_Tempe;

        public bool SetLHT3_Curt = false;
        public string LHT3_Curt;

        public bool SetLHT3_Volt = false;
        public string LHT3_Volt;

        public bool SetLHT4_Power = false;
        public string LHT4_Power;

        public bool SetLHT4_Freq = false;
        public string LHT4_Freq;

        public bool SetLHT4_Tempe = false;
        public string LHT4_Tempe;

        public bool SetLHT4_Curt = false;
        public string LHT4_Curt;

        public bool SetLHT4_Volt = false;
        public string LHT4_Volt;

        public bool SetQC = false;
        public string QC;

        public bool SetSQ_1 = false;
        public string SQ_1;

        public bool SetSQ_2 = false;
        public string SQ_2;

        public bool SetSQ_3 = false;
        public string SQ_3;

        public bool SetSQ_4 = false;
        public string SQ_4;

        /// <summary>
        /// 获取每个设备当前测量的stripNo 及其DTJ,从EM 开始顺序排列，若没有则不插入StripPosition类型
        /// 先获取每个设备的固定坐标（机械给出），然后算出每卷钢卷的DTJ, 由于一卷钢卷可能被剪断，甚至回滚，则额外获取STRIP_START STRIP_END 数据
        /// 通过一组 STRIP_START STRIP_END 计算出这组 START 和 EDN 之间的设备，然后计算出设备测量的数据对应STRIP 的DTJ
        /// </summary>
        /// <param name="stripNumber">当前有效的stripNo 队列，顺序从飞锯向EM 排列</param>
        /// <param name="machinePosition">当前设备的机械位置队列，顺序从EM 向飞锯排列</param>
        /// <returns></returns>
        public List<StripPosition> GetStripLocation(List<string> stripNumber, List<int> machinePosition)
        {
            //取出有效 dtj加入到 dtjList 中
            List<double> dtjList = new List<double>();
            double dtj = 0;
            //bool vaild = false;
            for (int i = 0; i < Dtj.Length; i++)
            {
                dtj = Dtj[i];
                if (dtj > 0)
                {
                    dtjList.Add(dtj);
                }
                else
                    break;

            }

            List<StripPosition> machineDtj = new List<StripPosition>();
            //当有效的dtj数量大于有效的stripNumber的时候，认为参数非法，返回空字典
            if (dtjList.Count > stripNumber.Count)
                return machineDtj;

            for (int i = 0; i < machinePosition.Count; i++)
            {
                double currentPosition = 0;
                int stripSeq = 0;
                //获得当前设备在哪个DTJ 管号下面
                for (int j = 0; j < dtjList.Count; j++)
                {
                    if (machinePosition[i] <= dtjList[j])
                    {
                        currentPosition = dtjList[j];
                        stripSeq = j;
                    }
                    else
                        break;
                }

                if (currentPosition > 0)
                {
                    StripPosition sp = new StripPosition(stripNumber[stripSeq], currentPosition - machinePosition[i]);
                    machineDtj.Add(sp);
                }
                else
                    break;

            }

            return machineDtj;
        }


        public List<StripPosition> GetRecordMachine(List<string> stripNumber, List<int> machinePosition)
        {

            List<StripPosition> stripPositions = new List<StripPosition>();

            for(int i=0;i<machinePosition.Count;i++)
            {
                for (int j = 0; j < stripNumber.Count; j++)
                {
                    if (machinePosition[i] < StripStart[j] && machinePosition[i] > StripEnd[j])
                    {
                        StripPosition sp = new StripPosition(stripNumber[j], Dtj[j] - machinePosition[i],i);
                        stripPositions.Add(sp);
                        break;
                    }
                }
            }
            return stripPositions;
        }

        public bool IsDataReady()
        {
            bool dtjSte = true;
            for (int i = 0; i < SetDtj.Length; i++)
            {
                dtjSte = dtjSte | SetDtj[i];
                if (!dtjSte)
                    break;
            }

            bool s = dtjSte &
                SetHFW_Power & SetHFW_Freq & SetHFW_Tempe & SetHFW_Curt & SetHFW_Volt &
                SetLHT1_Power & SetLHT1_Freq & SetLHT1_Tempe & SetLHT1_Curt & SetLHT1_Volt &
                SetLHT2_Power & SetLHT2_Freq & SetLHT2_Tempe & SetLHT2_Curt & SetLHT2_Volt &
                SetLHT3_Power & SetLHT3_Freq & SetLHT3_Tempe & SetLHT3_Curt & SetLHT3_Volt &
                SetLHT4_Power & SetLHT4_Freq & SetLHT4_Tempe & SetLHT4_Curt & SetLHT4_Volt &
                SetQC &
                SetSQ_1 & SetSQ_2 & SetSQ_3 & SetSQ_4;
            return s;
        }

        public void ResetStatus()
        {
            for (int i = 0; i < SetDtj.Length; i++)
            {
                SetDtj[i] = false;
            }

            SetHFW_Power = false;
            SetHFW_Freq = false;
            SetHFW_Tempe = false;
            SetHFW_Curt = false;
            SetHFW_Volt = false;

            SetLHT1_Power = false;
            SetLHT1_Freq = false;
            SetLHT1_Tempe = false;
            SetLHT1_Curt = false;
            SetLHT1_Volt = false;

            SetLHT2_Power = false;
            SetLHT2_Freq = false;
            SetLHT2_Tempe = false;
            SetLHT2_Curt = false;
            SetLHT2_Volt = false;

            SetLHT3_Power = false;
            SetLHT3_Freq = false;
            SetLHT3_Tempe = false;
            SetLHT3_Curt = false;
            SetLHT3_Volt = false;

            SetLHT4_Power = false;
            SetLHT4_Freq = false;
            SetLHT4_Tempe = false;
            SetLHT4_Curt = false;
            SetLHT4_Volt = false;

            SetQC = false;

            SetSQ_1 = false;
            SetSQ_2 = false;
            SetSQ_3 = false;
            SetSQ_4 = false;

        }
    }

    public class StripPosition
    {
        public StripPosition()
        {
            stripNumber = "0";
            position = 0;
            machineId = 0;
        }

        public StripPosition(string stripNo, double pos)
        {
            stripNumber = stripNo;
            position = pos;
        }

        public StripPosition(string stripNo, double pos,int id)
        {
            stripNumber = stripNo;
            position = pos;
            machineId = id;
        }

        public string stripNumber;
        public double position;
        public int machineId;
    }

    /// <summary>
    /// 设备状态改变时间，用来启停process data 的记录
    /// </summary>
    public class MachineStatusChangedEventArgs : EventArgs
    {
        public bool Status { get; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sessionAdr"></param>
        /// <param name="action">true=add;false=remove</param>
        public MachineStatusChangedEventArgs(bool status)
        {
            Status = status;
        }
    }

}
